	gnMapHeight = gnMapWidth = gnRowDesde = gnRowHasta= gnColDesde= gnColHasta= gnX1 = gnY1 = gnMapStartY = gnMapStartX = gnMapPosX = gnMapPosY = 0;
	gnMapWidthPopUp = 500;gnMapHeightPopUp =440;
	gnTool = 1; 
	gnZoom = 5;
	gnScalePointPx = 5;
	gbFlagMapMove = false;
	gcBlockMove=false;
	//gcMapImages ='imagenes/mapa';
	gaPoints = Array();
	window.onresize=resizeMap;
 
	function refreshElementMap()
	{
		if (goXMLHttpRequest.readyState==4)
		{
			refreshElement();
			oMap = id( "map" );
			if ( oMap ) loadMap();
		}
	}

	function mapTagClickEvent(e, link, nModo)
	{
		if (!e) var e = window.event;
		switch (e.button )
		{
			case 0: // Boton Izquierdo
				if (link) 
				{
					if (nModo == 1) 
						openPopup( link, ' ', gnMapWidthPopUp, gnMapHeightPopUp );
					else
						send( link, refreshElement, "body");
				}
			break;
			case 1: // Boton derecho
				
			break;
		}
	}

	function refreshTags()
	{
		var cDiv = '';
		coef = Math.pow( 2, 5 - gnZoom );
		nMapPointPx = gnScalePointPx * ( gnZoom );
		oDiv = id( "tags" );
		for (var i = 0; i < gaPoints.length; i++)
		{
			x  = gaPoints[i][0] / coef ;
			y  = gaPoints[i][1] / coef ;
			cNombre  = gaPoints[i][2];
			cLink  = gaPoints[i][3];
			nModo  = gaPoints[i][4];
			cImagen  = gaPoints[i][5] ? gaPoints[i][5] : 'mapa_punto.png';
			
			s = gcMapImages + '/mapa/' + cImagen;
			l = x - (nMapPointPx/2);
			t = y - (nMapPointPx/2);
			cDiv += '<a href=\'javascript: void(0);\' onclick=\'mapTagClickEvent( event, "' + cLink + '", ' + nModo + '); return false;\' ><img alt="' + cNombre + '" title="' + cNombre + '" src="' + s + '" style="border: 0px none ; margin: 0px; padding: 0px; position: absolute; left: ' + l + 'px; top: ' + t + 'px; width: ' + nMapPointPx + 'px; height: ' + nMapPointPx + 'px; -moz-user-select: none;" /></a>\n';
		}
		if ( cDiv ) 	oDiv.innerHTML  = cDiv;
		//<img src="" .  URL_IMAGES .  "/mapa_punto.png\" style=\"border: 0px none ; margin: 0px; padding: 0px; position: absolute; left: " . ($x-25)  . "px; top: " . ($y - 25)  . "px; width: 51px; height: 51px; -moz-user-select: none;\" />
	}
	function resizeMap()
	{
		oMap = id( "map" );
		if ( oMap )
		{
			w = oMap.parentNode.clientWidth;
			h = oMap.parentNode.clientHeight;
			if ( h == 0 ) h = document.documentElement.clientHeight;
			resize( w, h );
			x = (8192/2) - (w/2)
			y = (8192/2) - (h/2)
			refreshMap( x, y );
			refreshTags();
		}
	}
	function mapX(e)
	{
		nX = gnX1 + e.pageX - gnMapPosX ;
		coef = Math.pow( 2, 5 - gnZoom );
		return nX*coef;
	}
	function mapY(e)
	{
		nY = gnY1 + e.pageY - gnMapPosY ;
		coef = Math.pow( 2, 5 - gnZoom );
		return nY*coef;
	}
	function loadMap(cRootImage)
	{
		gnMapHeight = gnMapWidth = gnRowDesde = gnRowHasta= gnColDesde= gnColHasta= gnX1 = gnY1 = gnMapStartY = gnMapStartX = gnMapPosX = gnMapPosY = 0;
		if ( cRootImage ) gcMapImages = cRootImage;
		gnTool = 1;
		gbFlagMapMove = false;

		// Configura los parametros
		oMapValues = id( "map_values" );
		if ( oMapValues )
		{
			aMapValues = oMapValues.value.split("|");
			gnZoom = parseInt(aMapValues[2]);
			coef = Math.pow( 2, 5 - gnZoom );
			w = parseInt(aMapValues[0]);
			h = parseInt(aMapValues[1]);
			a = parseInt(aMapValues[3])/ coef;
			b = parseInt(aMapValues[4])/ coef;
			if ( parseInt(aMapValues[5]) > 0  ) gnMapWidthPopUp = parseInt(aMapValues[5]);
			if ( parseInt(aMapValues[6]) > 0  ) gnMapHeightPopUp = parseInt(aMapValues[6]);
		} else
		{
			oMap = id( "map" );
			w = oMap.parentNode.clientWidth;
			h = oMap.parentNode.clientHeight;
			if ( h == 0 ) h = document.documentElement.clientHeight;
			a = (8192/2) - (w/2);
			b = (8192/2) - (h/2);
		}
		resize( w, h );
		oTags = id('map_tags');
		aTags = oTags.value.split("|");
		gaPoints.length = 0;
		for( i = 0 ; i < aTags.length; i++ )
		{
			if ( aTags[i] )	gaPoints[i] = aTags[i].split(",");
		}
		
		refreshMap( a, b );
		refreshTags();
		center ( a, b );
	}
	
	function resize ( w, h )
	{
		obj = oMap = id( "map" );
		oMap.style.height = h + 'px';
		oMap.style.width = w + 'px';
		gnMapWidth = w;
		gnMapHeight = h;
		gnMapPosY= gnMapPosX = 0 ;
		while ( obj = obj.offsetParent  )
		{
			gnMapPosX += obj.offsetLeft;
			gnMapPosY+= obj.offsetTop;
		}
	}

	function center ( a, b )
	{
		if ( isNaN(a) || isNaN(b) ) return false;
		refreshMap ( a - (gnMapWidth/2), b - (gnMapHeight /2) ) ;
	}
	function changeTool( t )
	{
		gnTool = t;
		//oToolbar = id( "toolbar" );
	}
	function rollOutZoom ( oThis, z )
	{
		if ( z != gnZoom)  oThis.style.backgroundImage =  'url(' + gcMapImages +'/mapa/z' + z + '.gif )';
	}
	function rollOverZoom ( oThis, z )
	{
		if ( z != gnZoom)  oThis.style.backgroundImage =  'url(' + gcMapImages +'/mapa/z' + z + '_over.gif )';
	}
	function changeMapZoom ( z )
	{
		if ( isNaN(z) )  return false;
		if ( gnZoom == z ) return false;
		oButton = id( 'button_zoom_' + gnZoom);
		oButton.style.backgroundImage =  'url(' + gcMapImages +'/mapa/z' + gnZoom + '.gif )';
		oButton = id( 'button_zoom_' + z);
		oButton.style.backgroundImage =  'url(' + gcMapImages +'/mapa/z' + z + '_over.gif )';
		coef = Math.pow( 2, z - gnZoom );
		gnMapStartX *= coef;
		gnMapStartY *= coef;
		a = coef * gnX1 + gnMapWidth * ( coef -1 ) / 2;
		b = coef * gnY1 + gnMapHeight * ( coef -1 ) / 2;
		gnZoom = z;
		gnRowDesde = 0;
		gnRowHasta= 0;
		gnColDesde= 0;
		gnColHasta= 0;
		refreshMap( a, b ) ;
		refreshTags() ;
		return true;
	}
	function mapChangeCanvas(x, y)
	{
		oCanvas = id( 'canvas' );
		oCanvas.style.left = "-" + x + "px";
		oCanvas.style.top = "-" + y + "px";
	}
	function move( a, b )
	{
		if ( gcBlockMove ) return false;
		gcBlockMove=true;
		oCanvas = id( "canvas" );
		incx = (a - gnX1) / 25;
		incy = (b - gnY1) / 25;
		for( x = gnX1, y= gnY1, n= 0; n < 25 ; x+= incx, y += incy, n++ )
		{
			setTimeout( "mapChangeCanvas(" + x + ", " + y + ");", n * 50);
		}
		gnX1 = x;
		gnY1 = y;
		setTimeout( "refreshMap(" + a + ", " + b + ");gcBlockMove=false", n * 50);
	}
	function refreshMap( a, b )
	{
		oCanvas = id( "canvas" );
		oImages = id( "images" );
		gnX1 = a;
		gnY1 = b;
		x2 = gnX1 + gnMapWidth;
		y2 = gnY1 + gnMapHeight;
		nMax  = 256 * Math.pow( 2, gnZoom );
		//if ( x2 > nMax  || y2 > nMax   ) return false;
		c1 = Math.floor( gnX1 / 256) ;
		c2 = Math.ceil(x2 / 256) ;
		r1 = Math.floor(gnY1 / 256) ;
		r2 = Math.ceil(y2 / 256) ;
		if ( c1 < 1 ) c1 = 1;
		if ( r1 < 1 ) r1 = 1;
		switch ( gnZoom )
		{
			case 5: fileprefix = gcMapImages + '/8192/8192_r'; maxc=32 ;maxr =32 ;break;
			case 4: fileprefix = gcMapImages + '/4096/4096_r'; maxc=16 ;maxr =16 ;break;
			case 3: fileprefix = gcMapImages + '/2048/2048_r'; maxc= 8 ;maxr = 8 ;break;
			case 2: fileprefix = gcMapImages + '/1024/1024_r'; maxc= 4 ;maxr = 4 ;break;
			case 1: fileprefix = gcMapImages + '/512/512_r';   maxc= 2 ;maxr = 2 ;break;
		}
		if ( c2 > maxc ) c2 = maxc;
		if ( r2 > maxc ) r2 = maxr;
		oCanvas.style.left = (-gnX1) + 'px';
		oCanvas.style.top = (-gnY1) + 'px';
		if ( c1 < gnColDesde || c2 > gnColHasta || r1 < gnRowDesde || r2 > gnRowHasta || isExplorer() )
		{
			gnColDesde = c1;
			gnColHasta = c2;
			gnRowDesde = r1;
			gnRowHasta = r2;
			cDiv = "";
			for( x = gnX1, i = c1; i <= c2; i++, x+= 256 )
			{
				for ( y=gnY1, j = r1; j <= r2; j++, y+= 256 )
				{
					s = fileprefix + j + '_c' + i + ".jpg";
					l = ( i - 1 ) * 256;
					t = ( j - 1 ) * 256;
					cDiv += '<img  onmousedown="return false;" src="' + s + '" style="border: 0px none ; margin: 0px; padding: 0px; position: absolute; left: ' + l + 'px; top: ' + t + 'px; width: 256px; height: 256px; -moz-user-select: none;" alt="imagen mapa" />\n';
				}
			}
			oImages.innerHTML = cDiv;
		} else
		{
			//oImages.innerHTML = oImages.innerHTML;
		}
	}
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// Eventos del Mapa
	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function mapDblclickEvent(e)
	{
		if (!e) var e = window.event;
		if ( gnZoom < 5 ) changeMapZoom ( gnZoom + 1 ) ;
		center( gnX1 + e.clientX - gnMapPosX, gnY1 + e.clientY - gnMapPosY);
	}
	function mapMouseoutEvent( e , oThis)
	{
		if (!e) var e = window.event;
		if ( gbFlagMapMove )
		{
			if ( (  e.clientX <= gnMapPosX || e.clientX > (gnMapWidth + gnMapPosX) ) ||  (  e.clientY <= gnMapPosY || e.clientY > (gnMapHeight + gnMapPosY) ) )
			{
				mapMouseupEvent( e , oThis);
			}
		}
	}
	function mapMousedownEvent(e, oThis)
	{
		if (!e) var e = window.event;
		gbFlagMapMove = true;
		gnMapStartX = e.clientX ;
		gnMapStartY = e.clientY ;
		oThis.style.cursor = "move";
	}
	function mapMouseupEvent(e, oThis)
	{
		gbFlagMapMove = false;
		gnMapStartX = gnMapStartY = 0;
		oThis.style.cursor = "default";
	}
	function mapMousemoveEvent(e, oThis)
	{
		if (!e) var e = window.event;
		if ( gbFlagMapMove)
		{
			difX = gnMapStartX - e.clientX ;
			difY = gnMapStartY - e.clientY ;
			gnMapStartX = e.clientX ;
			gnMapStartY = e.clientY ;
			refreshMap( gnX1 + difX, gnY1 + difY );
		}
		nX = gnX1 + e.pageX - gnMapPosX ;
		nY = gnY1 + e.pageY - gnMapPosY ;
		coef = Math.pow( 2, 5 - gnZoom );
		oMapCoord = id( "map_coord" );
		if ( oMapCoord ) oMapCoord.innerHTML = '('  + parseInt(nX*coef)  + ','  + parseInt(nY*coef) + ')';
		return true;
	}
